<?php
/**
 * @version 1.0
 * @since Aug-2008
 * @package lib
 * @subpackage vimua
 * @name CUploadProduct
 * @deprecated Lớp thao tác với ảnh đã upload
*/
require_once ROOT_DIR.'3rdparty/Asido/class.asido.php';
require_once ROOT_DIR.'lib/utils/CUpload.php';
require_once ROOT_DIR.'lib/utils/CImage.php';
class CLocalUploadImage extends CUpload{
	function __construct($dir_name, $input_name, $prefix) {
		if (extension_loaded('imagick')){
			asido::driver('imagick_ext');
		} else {
			asido::driver('gd');
		}
		parent::__construct($dir_name, $input_name, $prefix);
	}

	function upload($allowed_types = '', $sync_files = true) {
		if (($files = parent::upload($allowed_types)) && $sync_files) {
			$root = CFile('', true);
			foreach ($files as $name=>$file) {
				$root->sync_files(array($file));
			}
		}
		return $files;
	}

	function make_multi_size_image($image_file, $base_dir, $image_sizes = null) {
		if ($image_sizes == null) {
			$image_sizes = array(
				'small' => array(100, 100),
				'medium' => array(200, 200)
			);
		}

		$thumbnails = array();
		$sync_dir = CFile($base_dir, true);
		$image_dir = $sync_dir->mkdir();
		$sync_files = array();
		$time = time();
		foreach ($image_sizes as $size=>$value) {
			$name = $size.'_'.$time;
			$i = Asido::Image(ROOT_DIR.$image_file, ROOT_DIR.$image_dir.$name.'.jpg');
			Asido::Frame($i, $value[0], $value[1]);
			$i->Save(ASIDO_OVERWRITE_ENABLED);
			$thumbnails[$size] = '/'.$image_dir.$name.'.jpg';
			$sync_files[] = $name.'.jpg';
		}
		$sync_dir->sync_files($sync_files);
		return $thumbnails;
	}

	function upload_product_image(&$thumbnails){
		$image_sizes = array(
				'small'=> array(68,148),
				'medium'=>array(98,213),
				'large'=>array(120,261)
				);
		$list_images = $this->upload(IMAGE_EXTENSION, true);
		$sync_dir = array();
		$image_dir = array();
		$sync_files = array();
		if(!empty($list_images)){
			$portal_id = portal()->id;
			foreach ($image_sizes as $size=>$value) {
				$sync_dir[$size] = CFile('upload/'.$portal_id.'/product/'.$size.'/'.date('Ymd').'/', true);
				$image_dir[$size] = $sync_dir[$size]->mkdir();
				$sync_files[$size] = array();
			}
			foreach ($list_images as $key1=>$val) {
				foreach ($image_sizes as $key2=>$size){
					$i = Asido::Image($val, $image_dir[$key2].$key1.'.jpg');
					Asido::Frame($i, $size[0], $size[1]);
					$i->Save(ASIDO_OVERWRITE_ENABLED);
					$thumbnails[$key2][$key1] = $image_dir[$key2].$key1.'.jpg';
					$sync_files[$key2][] = $key1.'.jpg';
				}
			}

			// quang's
			// sync all images to all sync folders
			error_log('sync_files: '.var_export($sync_files)."\n", 3, ROOT_DIR.'publish/sync.txt');
			foreach ($sync_dir as $size=>$value) {
				$value->sync_files($sync_files[$size]);
			}
			return $list_images;
		} else {
			return false;
		}
	}

	function upload_category_image(){
		$list_images = $this->upload(IMAGE_EXTENSION);
		$folder = CFile('upload/'.portal()->id.'/thumbnail/'.date('Ymd').'/',true);
		$thumb_dir = $folder->mkdir();
		foreach ($list_images as $key=>$val) {
			$i = Asido::Image($val, $thumb_dir.$key);
			Asido::Frame($i, 132, 116);
			$i->Save(ASIDO_OVERWRITE_ENABLED);
			$folder->sync_files(array($key));
		}
		return $list_images;
	}

	function upload_avatar_image(){
		$list_images = $this->upload(IMAGE_EXTENSION);
		$image_file_names = array();
		if(!empty($list_images)){
			$errors = array();
			foreach ($list_images as $key=>$val) {
				if(CImage::check_image($val)){
					$i = Asido::Image($val, $val);
					Asido::Frame($i, 80, 80);
					$i->Save(ASIDO_OVERWRITE_ENABLED);
				} else {
					$errors[] = 'IMAGE_HEADER_ERROR';
				}
				$image_file_names[] = $val;
			}
			// ???????????????????????????????????????????????????
			// quang's
			$syn_files = CFile('', true);
			$syn_files->sync_files($image_file_names);
			if(empty($errors)){
				return $list_images;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	function upload_home_image(){
		$list_images = $this->upload(IMAGE_EXTENSION);
		$image_file_names = array();
		if(!empty($list_images)){
			$errors = array();
			foreach ($list_images as $key=>$val) {
				if(CImage::check_image($val)){
					$i = Asido::Image($val, $val);
					Asido::Frame($i, 25, 65);
					$i->Save(ASIDO_OVERWRITE_ENABLED);
				} else {
					$errors[] = 'IMAGE_HEADER_ERROR';
				}
				$image_file_names[] = $val;
			}
			// ???????????????????????????????????????????????????
			// quang's
			$syn_files = CFile('', true);
			$syn_files->sync_files($image_file_names);
			if(empty($errors)){
				return $list_images;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	function upload_image_by_sizes($image_sizes=false, $resize_config=false, $resize_sizes=false, $color=null){
		$list_images = $this->upload(IMAGE_EXTENSION);
		if(!empty($list_images)){
			if(!empty($resize_config) && !empty($resize_sizes)){
				switch ($resize_config){
					case 'width':
						$this->resize_by_width($list_images, $resize_sizes);
						break;
					case 'height':
						$this->resize_by_height($list_images, $resize_sizes);
						break;
				}
			}
			if(!empty($image_sizes)){
				foreach ($list_images as $key1=>$val1) {
					foreach ($image_sizes as $key2=>$size){
						$folder = CFile('upload/'.portal()->id.'/'.$key2.'/'.date('Ymd').'/',true);
						$size_dir = $folder->mkdir();
						$i = Asido::Image($val1, $size_dir.$key1);
						if(isset($color[$key2])){
							$obj_color = new Asido_Color;
							$obj_color->set(intval($color[$key2]['red']),intval($color[$key2]['green']),intval($color[$key2]['blue']));
						} else {
							$obj_color = null;
						}
						Asido::Frame($i, intval($size), intval($size),$obj_color);
						$i->Save(ASIDO_OVERWRITE_ENABLED);
						$folder->sync_files(array($key1));
					}
				}
			}
			return $list_images;
		} else {
			return false;
		}
	}

	function resize_by_width($list_images, $image_sizes){
		if(!empty($list_images) && !empty($image_sizes)){
			foreach ($list_images as $key1=>$val1) {
				foreach ($image_sizes as $key2=>$size){
					$folder = CFile('upload/'.portal()->id.'/'.$key2.'/'.date('Ymd').'/',true);
					$size_dir = $folder->mkdir();
					$i = Asido::Image($val1, $size_dir.$key1);
					Asido::width($i, intval($size));
					$i->Save(ASIDO_OVERWRITE_ENABLED);
					$folder->sync_files(array($key1));
				}
			}
		}
	}

	function resize_by_height($list_images, $image_sizes){
		if(!empty($list_images) && !empty($image_sizes)){
			foreach ($list_images as $key1=>$val1) {
				foreach ($image_sizes as $key2=>$size){
					$folder = CFile('upload/'.portal()->id.'/'.$key2.'/'.date('Ymd').'/',true);
					$size_dir = $folder->mkdir();
					$i = Asido::Image($val1, $size_dir.$key1);
					Asido::height($i, intval($size));
					$i->Save(ASIDO_OVERWRITE_ENABLED);
					$folder->sync_files(array($key1));
				}
			}
		}
	}
}
function CLocalUploadImage($dir_name, $input_name, $prefix){
	return new CLocalUploadImage($dir_name, $input_name, $prefix);
}
?>